﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sarnata.Net.BareFtp.Protocol;
using Sarnata.Net.BareFtp.Protocol.Ftp;
using System.IO;

namespace Sarnata.Net
{
    public class SimpleFtpHelper
    {
        public string FtpHost { get; set; }
        public string FtpUser { get; set; }
        public string FtpPassword { get; set; }
        public bool FtpPassive { get; set; }
        public int FtpPort { get; set; }

        private FTPConnection _ftpConnection = null;

        public SimpleFtpHelper()
        {
            FtpHost = String.Empty;
            FtpUser = String.Empty;
            FtpPassword = String.Empty;
            FtpPassive = true;
            FtpPort = 21;
        }

        private void connectToFTPServer(bool isSecure)
        {
           _ftpConnection = new FTPConnection(new ConnectionProperties
            {
                Hostname = FtpHost,
                Port = FtpPort,
                FTPS = isSecure,
                User = FtpUser,
                Password = FtpPassword,
                Passive = FtpPassive
            });
            _ftpConnection.Open();
        }
        
        private void disconnectFromFTPServer()
        {
            _ftpConnection.Close();
        }

        private bool localFileExists(string fileName)
        {
            return File.Exists(fileName);
        }

        private bool remoteFileExists(string fileName)
        {
            var fileList = _ftpConnection.Dir();
            foreach (var file in fileList)
                if (file == fileName)
                    return true;
            return false;
        }

        private void sendFile(string sourceFile, string targetDirectory, bool overwrite)
        {
            if (localFileExists(sourceFile))
            {
                _ftpConnection.SetCurrentDirectory(_ftpConnection.GetCurrentDirectory() + "/IN");
                var bp = new BarePath();
                bp.SetBaseRemote(_ftpConnection.GetCurrentDirectory());
                bp.SetBaseLocal(@"./out");
                bp.SetFileName("", "somefile");
                var xf = new XferFile(0, bp, 0, TransferDirection.Upload, false, FtpHost);
                _ftpConnection.SendFile(xf);
            }
        }

        private void receiveFile(string sourceFile, string targetDirectory, bool overwrite)
        {
            if (remoteFileExists(sourceFile))
            {
                
            }
        }

        public void UploadNormal(string sourceFile, string targetDirectory)
        {
            UploadNormal(sourceFile, targetDirectory, false);    
        }

        public void UploadNormal(string sourceFile, string targetDirectory, bool overwrite)
        {
            connectToFTPServer(false);
            sendFile(sourceFile, targetDirectory, overwrite);
            disconnectFromFTPServer();
        }

        public void UploadSecurely(string sourceFile, string targetDirectory)
        {
            UploadSecurely(sourceFile, targetDirectory, false);
        }

        public void UploadSecurely(string sourceFile, string targetDirectory, bool overwrite)
        {
            connectToFTPServer(true);
            sendFile(sourceFile, targetDirectory, overwrite);
            disconnectFromFTPServer();
        }

        public void DownloadNormal(string sourceFile, string targetDirectory)
        {
            DownloadNormal(sourceFile, targetDirectory, false);
        }

        public void DownloadNormal(string sourceFile, string targetDirectory, bool overwrite)
        {
            connectToFTPServer(false);
            receiveFile(sourceFile, targetDirectory, overwrite);
            disconnectFromFTPServer();
        }

        public void DownloadSecurely(string sourceFile, string targetDirectory)
        {
            DownloadSecurely(sourceFile, targetDirectory, false);
        }

        public void DownloadSecurely(string sourceFile, string targetDirectory, bool overwrite)
        {
            connectToFTPServer(true);
            receiveFile(sourceFile, targetDirectory, overwrite);
            disconnectFromFTPServer();
        }
    }
}
